package org.example.salesystem.controller;

import org.example.salesystem.entity.ResultBody;
import org.example.salesystem.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;

@RestController
@RequestMapping("/SearchGoods")
@CrossOrigin(origins="*")
public class SearchController {
    @Autowired
    private SearchService searchService;
    @RequestMapping("/searchGoods")
    public ResultBody searchGoods(@RequestBody Map<String, Object> map) {
        // 判断参数是否完整
        if (map.get("name")==null&&map.get("kind")==null) {
            return searchService.search();
        }
        // 当传 name 时，查询商品名
        if (map.get("name") !=null) {
            return searchService.searchByName(map);
        }
        // 当传 kind 时，查询商品种类
        if (map.get("kind") != null) {
            return searchService.searchByKind(map);
        }
        // 如果没有匹配的查询方式
        return new ResultBody(500, "未知查询方式", false);
    }

    }
